package ute.mvc.controller.portal.lecturer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.model.QuestionPool;
import ute.mvc.model.TestAndQuizz;
import ute.mvc.model.TestPart;
import ute.mvc.service.QuestionPoolService;
import ute.mvc.service.TestAndQuizzService;
import ute.mvc.service.TestPartService;
import ute.mvc.util.IdUtil;

@Controller
@RequestMapping(value = "/lecturer/{lecturerId}/{openCourseId}/{subjectId}/{testId}/part")
public class PartController {

	@Autowired
	QuestionPoolService poolService;
	@Autowired
	TestAndQuizzService testService;
	@Autowired
	TestPartService partService;

	/**
	 * Test All OK
	 * 
	 */
	@RequestMapping(value = { "/add" }, method = RequestMethod.POST)
	@ResponseBody
	public TestPart addPart(@PathVariable String testId,
			@RequestBody TestPart part) {

		System.out.println("Add part: " + testId);
		TestAndQuizz test = testService.getTestAndQuizz(testId);

		// Get partId in test
		String maxId = partService.getMaxIdInTest(test);
		String newId = IdUtil.getNewPartIdInTest(maxId);
		part.setTestPartId(testId + "_" + newId);
		part.setTestAndQuizz(test);

		partService.addTestPart(part);
		return part;
	}

	@RequestMapping(value = { "/edit/{partId}" }, method = RequestMethod.GET)
	@ResponseBody
	public TestPart getPartInfo(@PathVariable String partId) {

		System.out.println("Get Part Info: " + partId);
		TestPart part = partService.getTestPartById(partId);
		return part;
	}

	// Update name of part
	@RequestMapping(value = { "/edit/{partId}" }, method = RequestMethod.POST)
	@ResponseBody
	public TestPart updatePart(@PathVariable String partId,
			@RequestBody TestPart part) {

		System.out.println("Edit Part " + partId);
		TestPart oldPart = partService.getTestPartById(partId);
		part.setQuestionPool(oldPart.getQuestionPool());
		part.setTestAndQuizz(oldPart.getTestAndQuizz());
		part.setPoint(oldPart.getPoint());
		part.setNumberQuestion(oldPart.getNumberQuestion());

		partService.updateTestPart(part);
		return part;
	}

	// Update pool id of part
	@RequestMapping(value = { "/editpoolid/{partId}" }, method = RequestMethod.POST)
	@ResponseBody
	public TestPart updatePoolPart(@PathVariable String partId,
			@RequestBody TestPart part) {

		System.out.println("Edit Part Pool " + partId);
		QuestionPool pool = poolService.getQuestionPool(part.getTestPartId());
		TestPart oldPart = partService.getTestPartById(partId);
		//Set pool
		part.setQuestionPool(pool);
		part.setTestAndQuizz(oldPart.getTestAndQuizz());
		//Set old pool id
		part.setTestPartId(partId);

		partService.updateTestPart(part);
		return part;
	}

	@RequestMapping(value = { "/delete/{partId}" }, method = RequestMethod.GET)
	@ResponseBody
	public String deletePart(@PathVariable String partId) {
		System.out.println("Delete Part: " + partId);
		TestPart part = partService.getTestPartById(partId);
		partService.deleteTestPart(part);
		return "success";
	}

}
